package com.zy.yun.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.constant.Const;
import com.zy.yun.base.constant.StatusEnum;
import com.zy.yun.base.entity.Res;
import com.zy.yun.base.entity.Role;
import com.zy.yun.base.entity.RoleRes;
import com.zy.yun.provider.service.IResService;
import com.zy.yun.provider.service.IRoleResService;
import com.zy.yun.provider.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: 王彦委
 * @Description: 把数据库中的表缓存到redis中
 * @Date: 21:17 2019/11/7
 */
@Component
public class TableCacheUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IResService resService;

    @Autowired
    private IRoleResService roleResService;

    @Autowired
    private IRoleService roleService;


    /**
      * @Author: 王彦委
      * @Description: 设置 表缓存到redis中
      * @Date: 22:56 2019/11/7
      */
    public  void setTableCache(){

        //只查询“启用”的资源
        EntityWrapper<Res> resQuery = new EntityWrapper<Res>();
        resQuery.eq("status",StatusEnum.STATUS_START.getCode());
        List<Res> resList = resService.selectList(resQuery);
        List<RoleRes> roleResList = roleResService.selectList(null);

        // 把这几张表放入redis缓存中
        stringRedisTemplate.opsForHash().put(Const.DATABASE_NAME, Const.TABLE_SYS_RES, JSONObject.toJSONString(resList));
        stringRedisTemplate.opsForHash().put(Const.DATABASE_NAME, Const.TABLE_SYS_ROLE_RES, JSONObject.toJSONString(roleResList));


    }

}
